import me.w70.bot.Bot;
import me.w70.bot.scripting.Random;
import me.w70.bot.wrappers.RSInterface;
import me.w70.bot.wrappers.RSInterfaces;
import me.w70.bot.wrappers.RSNPC;
import me.w70.bot.wrappers.RSObject;


public class Mordaut extends Random {

	public class NextObjectQuestion {
		int One = -1;
		int Two = -1;
		int Three = -1;

		public NextObjectQuestion() {
		}

		public boolean arrayContains(final int[] arr, final int i) {
			boolean returnt = false;
			for (final int num : arr) {
				if (num == i) {
					returnt = true;
				}
			}

			return returnt;
		}

		public boolean clickAnswer() {
			int[] Answers;
			if ((Answers = returnAnswer()) == null) {
				return false;
			}

			for (int i = 10; i <= 13; i++) {
				if (arrayContains(Answers, nextObjectInterface.getChild(i)
						.getComponentID())) {
					return atInterface(nextObjectInterface.getChild(i));
				}
			}

			return false;
		}

		public boolean getObjects() {
			One = -1;
			Two = -1;
			Three = -1;
			One = nextObjectInterface.getChild(6).getComponentID();
			Two = nextObjectInterface.getChild(7).getComponentID();
			Three = nextObjectInterface.getChild(8).getComponentID();

			return One != -1 && Two != -1 && Three != -1;
		}

		public void guess() {
			final int[] objects = new int[4];
			objects[0] = nextObjectInterface.getChild(10).getComponentID();
			objects[1] = nextObjectInterface.getChild(11).getComponentID();
			objects[2] = nextObjectInterface.getChild(12).getComponentID();
			objects[3] = nextObjectInterface.getChild(13).getComponentID();

			int lowest = 120;
			int click = 10;
			final int compare = returnAnswer()[0];
			if (compare <= 10) {
				atInterface(nextObjectInterface.getChild(random(10, 13)));
				return;
			}

			for (int i = 0; i < objects.length; i++) {
				if (Math.abs(objects[i] - compare) <= lowest) {
					lowest = Math.abs(objects[i] - compare);
				}
				click = 10 + i;
			}

			atInterface(nextObjectInterface.getChild(click));
		}

		public int[] returnAnswer() {
			final int[] count = new int[items.length];
			for (int i = 0; i < count.length; i++) {
				count[i] = 0;
			}

			for (int i = 0; i < items.length; i++) {
				for (int j = 0; j < items[i].length; j++) {
					if (items[i][j] == One) {
						count[i]++;
					}
					if (items[i][j] == Two) {
						count[i]++;
					}
					if (items[i][j] == Three) {
						count[i]++;
					}

					if (count[i] >= 2) {
						Bot.addMessage(this, "Answer Type Found!");
						return items[i];
					}
				}
			}

			return null;
		}
	}

	public class SimilarObjectQuestion {
		String question;
		int[] Answers;

		public SimilarObjectQuestion(final String q, final int[] Answers) {
			question = q.toLowerCase();
			this.Answers = Answers;
		}

		public boolean accept() {
			return atInterface(relatedCardsInterface.getChild(26));
		}

		public boolean activateCondition() {
			if (!relatedCardsInterface.isValid()) {
				return false;
			}

			if (relatedCardsInterface.getChild(25).getText().toLowerCase()
					.contains(question)) {
				Bot.addMessage(this, "Question keyword: " + question);
				return true;
			}

			return false;
		}

		public boolean clickObjects() {
			int count = 0;
			for (int i = 42; i <= 56; i++) {
				for (final int answer : Answers) {
					if (relatedCardsInterface.getChild(i).getComponentID() == answer) {
						if (atInterface(relatedCardsInterface.getChild(i))) {
							try {
								wait(random(600, 1000));
							} catch (final Exception e) {
							}
						}
						count++;
						if (count >= 3) {
							return true;
						}
					}
				}
			}
			Bot.addMessage(this, "returns false");
			return false;
		}
	}

	public RSInterface nextObjectInterface = getInterface(103,0);

	public RSInterface relatedCardsInterface = getInterface(559,0);

	public int[] Ranged = { 11539, 11540, 11541, 11614, 11615, 11633 };

	public int[] Cooking = { 11526, 11529, 11545, 11549, 11550, 11555, 11560,
			11563, 11564, 11607, 11608, 11616, 11620, 11621, 11622, 11623,
			11628,

			11629, 11634, 11639, 11641, 11649, 11624 };

	public int[] Fishing = { 11527, 11574, 11578, 11580, 11599, 11600, 11601,
			11602, 11603,

			11604, 11605, 11606, 11625 };

	public int[] Combat = { 11528, 11531, 11536, 11537, 11579, 11591, 11592,
			11593, 11597, 11627, 11631, 11635, 11636, 11638, 11642, 11648,
			11617 };

	public int[] Farming = { 11530, 11532, 11547, 11548, 11554, 11556, 11571,
			11581, 11586, 11610, 11645 };

	public int[] Magic = { 11533, 11534, 11538, 11562, 11567, 11582 };

	public int[] Firemaking = { 11535, 11551, 11552, 11559, 11646 };

	public int[] Hats = { 11540, 11557, 11558, 11560, 11570, 11619, 11626,
			11630, 11632, 11637, 11654 };

	public int[] Drinks = { 11542, 11543, 11544, 11644, 11647 };

	public int[] Woodcutting = { 11573, 11595 };

	public int[] Boots = { 11561, 11618, 11650, 11651 };

	public int[] Crafting = { 11546, 11553, 11565, 11566, 11568, 11569, 11572,
			11575, 11576, 11577, 11581, 11583, 11584, 11585, 11643, 11652,
			11653 };

	public int[] Mining = { 11587, 11588, 11594, 11596, 11598, 11609, 11610 };

	public int[] Smithing = { 11611, 11612, 11613 };
	public int[][] items = { Ranged, Cooking, Fishing, Combat, Farming, Magic,
			Firemaking, Hats, Drinks, Woodcutting, Boots, Crafting, Mining,
			Smithing };
	public int Key = 11589;
	public int Book = 11590;
	public int Bones = 11617; // Combat?
	public int Feather = 11624; // Cooking?
	public int Hook = 11626; // Added to hats for pirate stuff.
	public int Cape = 11627; // Added to combat (legends cape)
	// Missing 11640
	public int Talisman = 11643; // Added to crafting

	public int Candle = 11646; // WTF? (Firemaking)

	public int Vial = 11653; // Crafting?

	public SimilarObjectQuestion[] simObjects = {
			new SimilarObjectQuestion("I'm feeling dehydrated", Drinks),
			new SimilarObjectQuestion("quenched my thirst", Drinks),
			new SimilarObjectQuestion("light my fire", Firemaking),
			new SimilarObjectQuestion("fishy", Fishing),
			new SimilarObjectQuestion("fishing for answers", Fishing),
			new SimilarObjectQuestion("fish out of water", Drinks),
			new SimilarObjectQuestion("I'm feeling dehydrated", Drinks),
			new SimilarObjectQuestion("strange headgear", Hats),
			new SimilarObjectQuestion("tip my hat", Hats),
			new SimilarObjectQuestion("thinking cap", Hats),
			new SimilarObjectQuestion("wizardry here", Magic),
			new SimilarObjectQuestion("rather mystical", Magic),
			new SimilarObjectQuestion("abracada", Magic),
			new SimilarObjectQuestion("hide one's face", Hats),
			new SimilarObjectQuestion("shall unmask", Hats),
			new SimilarObjectQuestion("hand-to-hand", Combat),
			new SimilarObjectQuestion("melee weapon", Combat),
			new SimilarObjectQuestion("me hearties", Hats),
			new SimilarObjectQuestion("mighty pirate", Drinks),
			new SimilarObjectQuestion("mighty archer", Ranged),
			new SimilarObjectQuestion("as an arrow", Ranged),
			new SimilarObjectQuestion("shiny things", Crafting),

			// Default
			new SimilarObjectQuestion("range", Ranged),
			new SimilarObjectQuestion("arrow", Ranged),
			new SimilarObjectQuestion("drink", Drinks),
			new SimilarObjectQuestion("logs", Firemaking),
			new SimilarObjectQuestion("light", Firemaking),
			new SimilarObjectQuestion("headgear", Hats),
			new SimilarObjectQuestion("hat", Hats),
			new SimilarObjectQuestion("cap", Hats),
			new SimilarObjectQuestion("mine", Mining),
			new SimilarObjectQuestion("mining", Mining),
			new SimilarObjectQuestion("ore", Mining),
			new SimilarObjectQuestion("fish", Fishing),
			new SimilarObjectQuestion("fishing", Fishing),
			new SimilarObjectQuestion("thinking cap", Hats),
			new SimilarObjectQuestion("cooking", Cooking),
			new SimilarObjectQuestion("cook", Cooking),
			new SimilarObjectQuestion("bake", Cooking),
			new SimilarObjectQuestion("farm", Farming),
			new SimilarObjectQuestion("farming", Farming),
			new SimilarObjectQuestion("cast", Magic),
			new SimilarObjectQuestion("magic", Magic),
			new SimilarObjectQuestion("craft", Crafting),
			new SimilarObjectQuestion("boot", Boots),
			new SimilarObjectQuestion("chop", Woodcutting),
			new SimilarObjectQuestion("cut", Woodcutting),
			new SimilarObjectQuestion("tree", Woodcutting),

	};

	public RSObject door = null;


	@Override
	public boolean canRun() {
		door = null;
		final RSNPC mordaut = getNearestNPCByName("Mr. Mordaut");
		if (mordaut != null) {
			return true;
		}
		return false;
	}

	@Override
	public int loop() {
		final RSNPC mordaut = getNearestNPCByName("Mr. Mordaut");
		if (mordaut == null) {
			return -1;
		}

		while (getMyPlayer().isMoving() || getMyPlayer().getAnimation() != -1) {
			return random(800, 1200);
		}

		if (door != null) {
			if (!atObject(door, "Open")) {
				setCameraAngle(random(0, 359));
				return random(800, 1200);
			}
		}

		final RSInterface inter = searchInterfacesText("door");
		if (inter != null) {
			if (inter.getText().toLowerCase().contains("red")) {
				door = findObject(2188);
			}
			if (inter.getText().toLowerCase().contains("green")) {
				door = findObject(2193);
			}
			if (inter.getText().toLowerCase().contains("blue")) {
				door = findObject(2189);
			}
			if (inter.getText().toLowerCase().contains("purple")) {
				door = findObject(2192);
			}
		}

		if (nextObjectInterface.isValid()) {
			Bot.addMessage(this, "Question Type: Next Object");
			final NextObjectQuestion noq = new NextObjectQuestion();
			if (noq.getObjects()) {
				if (noq.clickAnswer()) {
					return random(800, 1200);
				} else {
					noq.guess();
					return random(800, 1200);
				}
			} else {
				Bot.addMessage(this, "Could not find get object. Making educated guess.");
				noq.guess();
				return random(800, 1200);
			}
		}

		if (relatedCardsInterface.isValid()) {
			Bot.addMessage(this, "Question Type: Similar Objects");
			for (final SimilarObjectQuestion obj : simObjects) {
				if (obj.activateCondition()) {
					if (obj.clickObjects()) {
						obj.accept();
					}
				}
			}
			return random(800, 1200);
		}


			wait(5000);
			if (getMyPlayer().getInteracting() == null) {
				if (mordaut != null) {
					atNPC(mordaut, "Talk-to");
				}
			}
		

		if (clickContinue()) {
			return random(800, 3500);
		}

		return random(800, 1200);
	}

	public RSInterface searchInterfacesText(final String string) {
		final RSInterface[] inters = RSInterfaces.getAllInterfaces();
		for (final RSInterface inter : inters) {
			for (final RSInterface interfaceChild : inter.getChildren()) {
				if (interfaceChild.getText().toLowerCase().contains(
						string.toLowerCase())) {
					return interfaceChild;
				}
			}
		}

		return null;
	}


	@Override
	public String getScriptAuthor() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getScriptCategory() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getScriptDescription() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getScriptName() {
		// TODO Auto-generated method stub
		return null;
	}
}